package com.java0.dbshardingsphere02.config;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;

import java.util.Properties;
import java.util.concurrent.atomic.LongAdder;

/**
 * 自定义随机生成主键，不用考虑性能等其他
 * @author L.xf
 * @date 2021/10/30
 */
public final class RandomShardingKeyGenerator implements ShardingKeyGenerator {

    private static final LongAdder longAdder = new LongAdder();

    @Getter
    @Setter
    private Properties properties = new Properties();

    @Override
    public Comparable<?> generateKey() {
        longAdder.increment();
        return System.currentTimeMillis() + longAdder.longValue();
    }

    @Override
    public String getType() {
        return "RANDOM";
    }


}
